package com.kaifamiao.set;

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class TreeSetTest {

	public static void main(String[] args) {

		// 构造一个新的空 set，该 set 根据其元素的自然顺序进行排序
		TreeSet<Integer> ts = new TreeSet<>();

		ts.add(10);
		ts.add(8);
		ts.addAll(Set.of(9, 7, 10, 3, 5, 6, 4));

		System.out.println(ts);

		// 使用匿名类实现java.util.Comparator接口并完成对Goods实例的比较
		Comparator<Goods> comparator = new Comparator<>() {
			@Override
			public int compare(Goods a, Goods b) {
				if (a != null && a.getId() != null && b != null && b.getId() != null) {
					return a.getId() - b.getId();
				}
				return 0;
			}
		};

		TreeSet<Goods> gs = new TreeSet<>( comparator );

		Goods p1 = new Goods(1, "老干妈", 1200, 90, "瓶");
		gs.add(p1);

		Goods p2 = new Goods(1, "老干妈", 1200, 90, "瓶");
		gs.add(p2);
		
		Goods p3 = new Goods(2, "老干爹", 1200, 90, "瓶");
		gs.add(p3);
		
		System.out.println(gs);

	}

}
